package com.example.backdemo.service.Impl;

import com.example.backdemo.mapper.ActionMapper;
import com.example.backdemo.pojo.Aum;
import com.example.backdemo.pojo.IOmoney;
import com.example.backdemo.pojo.IOsum;
import com.example.backdemo.pojo.Message;
import com.example.backdemo.service.ActionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class ActionServiceImpl implements ActionService {

    @Resource
    private ActionMapper actionMapper;

    @Override
    public List<Aum> getAumAvg() {
        System.out.println(actionMapper.getAumAvg());
        return actionMapper.getAumAvg();
    }


    @Override
    public Map<String, Integer> getLabel() {
        List<Integer> list = actionMapper.getLabel();
        Map<String, Integer> map = new HashMap<>();
        String type1 = "下降";
        String type2 = "稳定";
        String type3 = "上升";
        map.put(type1, 0);
        map.put(type2, 0);
        map.put(type3, 0);
        for (Integer i : list) {
            if (i == -1) {
                int value = map.get(type1) + 1;
                map.put(type1, value);
            } else if (i == 0) {
                int value = map.get(type2) + 1;
                map.put(type2, value);
            } else if (i == 1) {
                int value = map.get(type3) + 1;
                map.put(type3, value);
            } else {
                System.out.println("错误数据，跳过");
            }
        }
        return map;
    }

    @Override
    public List<Message> getMessage() {
        return actionMapper.getMessage();
    }

    @Override
    public Map<String, Integer> getIOsum() {
        List<IOsum> list = actionMapper.getIOsum();
        Map<String, Integer> map = new HashMap<>();
        map.put("转入", 0);
        map.put("转出", 0);
        for (IOsum iOsum : list) {
            int value1 = map.get("转入") + iOsum.getB2();
            int value2 = map.get("转出") + iOsum.getB4();
            map.put("转入", value1);
            map.put("转出", value2);
        }
        return map;
    }

    @Override
    public Map<String, Float> getIOmoneyMax() {
        List<IOmoney> list = actionMapper.getIOmoney();
        float max1 = 0f;
        float max2 = 0f;
        for (IOmoney iOmoney : list) {
            max1 = Math.max(max1, iOmoney.getB3());
            max2 = Math.max(max2, iOmoney.getB5());
        }
        Map<String, Float> map = new HashMap<>();
        map.put("转入", max1);
        map.put("转出", max2);
        return map;
    }

    @Override
    public Map<String, Float> getIOmoneyAvg() {
        List<IOmoney> list = actionMapper.getIOmoney();
        float total1 = 0f;
        float total2 = 0f;
        for (IOmoney iOmoney : list) {
            total1 += iOmoney.getB3();
            total2 += iOmoney.getB5();
        }
        Map<String, Float> map = new HashMap<>();
        map.put("转入", total1 / list.size());
        map.put("转出", total2 / list.size());
        return map;
    }
}
